<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Group;
use Illuminate\Http\Request;

class GroupsController extends Controller
{
    public function index(Request $request)
    {
        $searchData = $request->post();

        $model = Group::orderBy('groups.id', 'DESC')
            ->leftJoin('users', 'users.id', '=', 'groups.user_id')
            ->select(['groups.*', 'users.nickname']);
        if ($nickname = $searchData['nickname'] ?? '') {
            $model->where('nickname', 'like', "%{$nickname}%");
        }
        if ($name = $searchData['name'] ?? '') {
            $model->where('groups.name', 'like', "%{$name}%");
        }
        $result = $model->paginate(config('app.limit'));

        return $this->writeJson(0, [
            'data' => $result->items(),
            'count' => $result->total()
        ]);
    }


}
